<template>
	<view class="container">
		<image class="image" src="https://oss.daccf.com/tuw8jf4we6d52sdl5umn5/全部/395b7989-0158-4a0c-994c-92ed0f032796.png" mode="">
			
		</image>
		<view class="content">
				<!-- <image class="logo" :src="wxConfig.accountInfo.icon" />
				<view class="title">{{ wxConfig.accountInfo.nickname }}</view>
				<view class="text">首次使用需要授权才能使用哦</view>
				<button class="userlogin" @click="getUserInfoTap" :style="{ backgroundColor: template.color.temp_color }">授权登录</button>
				<button class="userlogin navigateBack" :style="{ backgroundColor: template.color.temp_color + '20', color: template.color.temp_color }" @click="navigateBack()">
					暂不登录
				</button> -->
			
			<view class="box">
				<view class="title">
					学号登录
				</view>
				<view class="input">
					<uv-input v-model="idcard"  placeholder="请输入学号"></uv-input>
				</view>
				<view class="btn">
					<uv-button type="primary" text="立即登录"  @click="submit"></uv-button>
				</view>
				<view class="bott">
					<view class="left">
						<checkbox @click="changeBox()" :checked="checked" /><text>记住学号</text>
					</view>
					<view class="right" v-if="userId">
						用户ID：{{userId}}
					</view>
				</view>
			</view>
		</view>
	</view>
	
</template>

<script>
import { getCode, login ,uploadImages,updateUserInfo,getPlugsPracticeStudentByCard,updatePlugsPracticeStudent,userIsLogin,checkPracticeStudentBindingState} from '../../utils/api.js';
export default {
	data() {
		return {
			wxCode:'',
			idcard:uni.getStorageSync('studentCarId')||'',
			checked:true,
			info:{},
			userId:0
		};
	},
	onLoad() {
		this.getWechatLogin();
		this.userId=uni.getStorageSync('userId')
		// if(uni.getStorageSync('userInfo')){
		// 	this.userInfo=uni.getStorageSync('userInfo')
		// 	userIsLogin().then(res=>{
		// 		if(res.body.isLogin&&uni.getStorageSync('studentCarId')){
		// 			setTimeout(()=>{
		// 				uni.switchTab({
		// 					url:'/pages/index/index'
		// 				})
		// 				uni.hideLoading()
		// 			},100)
		// 		}
		// 	})
			
		// }else{
			
		// }
	},
	onShow() {
		// this.getWechatLogin();
		// if(uni.getStorageSync('userInfo')){
		// 	userIsLogin().then(res=>{
		// 		if(res.body.isLogin&&uni.getStorageSync('studentCarId')){
		// 			setTimeout(()=>{
		// 				uni.switchTab({
		// 					url:'/pages/index/index'
		// 				})
		// 				uni.hideLoading()
		// 			},100)
		// 		}
		// 		 else{
		// 			uni.removeStorage({
		// 				key:'userInfo'
		// 			})
		// 		}
		// 	})
		// 	// this.userInfo=uni.getStorageSync('userInfo')
		// 	// uni.showLoading({
		// 	// 	title: '正在登录中'
		// 	// })
		// 	// setTimeout(()=>{
		// 	// 	uni.switchTab({
		// 	// 		url:'/pages/index/index'
		// 	// 	})
		// 	// 	uni.hideLoading()
		// 	// },1000)
		// }else{
			
			
		// }
	},
	methods: {
		changeBox(){
			this.checked=!this.checked
			console.log(this.checked);
			uni.setStorage({
				key: 'checked',
				data: this.checked
			});
			
		},
		
		submit(){
			
			let data={
				studentCarId:this.idcard
			}
			getPlugsPracticeStudentByCard(data).then(res=>{
				if(res.body){
					if(res.body.enable==1){
						 if(this.checked){
						 	uni.setStorage({
						 		key: 'studentCarId',
						 		data: this.idcard
						 	});
						 }else{
						 	uni.removeStorage({
						 		key:'studentCarId'
						 	})
						 	
						 }
						 this.info=res.body
						 uni.setStorage({
						 	key: 'studentInfo',
						 	data: res.body
						 });
						 uni.setStorage({
						 	key: 'userId',
						 	data: res.body.id
						 });
						 this.getUserInfoTap()
					}else{
						uni.showToast({
							title: '当前账号已被禁用',
							icon:'none',
							duration: 800
						});
					}
					
				}else{
					uni.showToast({
						title: '当前身份证信息不存在',
						icon:'none',
						duration: 800
					});
				}
			})
		},
		
		
		
		
		getWechatLogin() {
			let that = this;
			uni.login({
				provider: 'weixin',
				success: function(wxs) {
					console.log(wxs,789);
					that.wxCode = wxs.code;
					console.log(that.wxCode,789);
				}
			});
		},
		getUserInfoTap() {
			let self = this;
			uni.login({
				provider: 'weixin',
				success: function(wxs) {
					
					console.log(wxs.code);
					uni.showLoading({
						title: '正在登录中'
					});
					
					let identification = '';
					getCode({
						code: wxs.code
					}).then(res => {
						identification = res.body.openid;
						let user = {
							identification: res.body.openid,
						};
						let postData = `wechatServerJSON=${JSON.stringify(user)}`;
						login(postData).then(wes => {
							if (wes.code == 200) {
								uni.hideLoading();
								uni.setStorage({
									key: '__ddminiUserOpenid',
									data: identification
								});
								uni.setStorage({
									key: 'userInfo',
									data: wes.body
								});
								uni.setStorage({
									key: 'userId',
									data: wes.body.id
								});
								checkPracticeStudentBindingState()
								.then(resss=>{
									if(!resss.body&&self.info.userid!==wes.body.id){
										self.info.userid=wes.body.id
										updatePlugsPracticeStudent(self.info).then(result=>{
											if(result.code==200){
												uni.showToast({
													title: '授权登录成功',
													duration: 800
												});
												uni.setStorage({
													key: '__ddminiUserOpenid',
													data: identification
												});
												uni.setStorage({
													key: 'userInfo',
													data: wes.body
												});
												uni.setStorage({
													key: 'userId',
													data: wes.body.id
												});
												setTimeout(()=>{
													uni.switchTab({
														url:'/pages/index/index'
													})
												},500)
											}
										})
									}else if(resss.body&&self.info.userid==resss.body.userid){
										uni.showToast({
											title: '授权登录成功',
											duration: 800
										});
										uni.setStorage({
											key: '__ddminiUserOpenid',
											data: identification
										});
										uni.setStorage({
											key: 'userInfo',
											data: wes.body
										});
										uni.setStorage({
											key: 'userId',
											data: wes.body.id
										});
										setTimeout(()=>{
											uni.switchTab({
												url:'/pages/index/index'
											})
										},500)
									}else if(resss.body&&self.info.userid!==resss.body.userid){
										uni.showToast({
											title: '当前身份证已被绑定',
											icon:'none',
											duration: 800
										});
										uni.removeStorageSync('studentInfo');
										uni.removeStorageSync('userInfo');
										uni.removeStorageSync('__ddminiUserOpenid');
										uni.removeStorageSync('__DC_STAT_UUID');
										self.getWechatLogin();
									}
								})
							} else {
								self.getWechatLogin();
								uni.hideLoading();
								uni.showModal({
									title: '授权失败',
									content: wes.msg,
									showCancel: false
								});
							}
						});
						
					});
					
				}
			});
			
		},
		
		navigateBack() {
			uni.navigateBack();
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #fff;
}

.image{
		width: 100%;
		height: 100vh;
	}
.content{
	position: fixed;
	bottom: 40%;
	left: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	height: 500;
	width: 100%;
	
}
.box{
	width: 550rpx;
	background-color: #fff;
	border-radius: 20rpx;
	padding: 20rpx 30rpx 40rpx;
	.title{
		text-align: center;
		padding: 30rpx;
		font-weight: 700;
	}
	.input{
		
	}
	.btn{
		margin: 30rpx 0;
	}
	.bott{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26rpx;
		
		text{
			margin-left: 10rpx;
		}
	}
}

</style>
